<?php

namespace app\shop\controller;

use think\Controller;

class Address extends Controller
{
    protected $busid;
    protected $Business;

    public function _initialize()
    {
        // 继承父类
        parent::_initialize();
        $this->BusinessModel = model('Business.Business');
        $this->RegionModel = model('Region');
        $this->AddressModel = model('Business.Address');

        $this->busid = $this->request->param('busid', 0, 'trim');

        $this->Business = $this->BusinessModel->find($this->busid);

        if (!$this->Business)
        {
            $this->error('用户不存在');
            exit;
        }
    }

    // 添加
    public function add()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->param();
            $code = isset($params['code']) ? $params['code'] : '';

            $data = [
                'busid' => $this->busid,
                'consignee' => $params['consignee'] ?? '',
                'address' => $params['address'] ?? '',
                'mobile' => $params['mobile'] ?? '',
                'status' => $params['status'] ?? '',
            ];

            $parentpath = $this->RegionModel->where('code', $code)->value('parentpath');

            if (!$parentpath)
            {
                $this->error('地区不存在，请重新选择');
                exit;
            }

            $path = explode(',', $parentpath);
            $data['province'] = $path[0] ?? '';
            $data['city'] = $path[1] ?? '';
            $data['district'] = $path[2] ?? '';

            // 判断是否添加默认地址
            if ($params['status'] == '1')
            {
                $ids = $this->AddressModel->where(['busid' => $this->busid, 'status' => '1'])->value('id');

                if ($ids)
                {
                    $res = $this->AddressModel->where('id', $ids)->update(['status' => '0']);

                    if (!$res)
                    {
                        $this->error('更新默认地址失败');
                        exit;
                    }
                }
            }

            // 校验器
            $result = $this->AddressModel->save($data);

            if ($result === false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }

            $this->success('添加成功');
            exit;
        }
    }

    // 列表
    public function index()
    {
        if ($this->request->isPost())
        {
            $data = $this->AddressModel->where(['busid' => $this->busid])->order('id desc')->select();

            $this->success('', '', $data);
        }
    }

    // 切换默认地址
    public function check()
    {
        if ($this->request->isPost())
        {
            $addid = $this->request->param('addid', 0, 'trim');

            $Address = $this->AddressModel->find($addid);

            if (!$Address)
            {
                $this->error('收件地址不存在');
                exit;
            }

            $res = $this->AddressModel->where(['busid' => $this->busid])->update(['status' => '0']);

            if ($res === false)
            {
                $this->error('更新默认地址失败');
                exit;
            }

            $result = $this->AddressModel->where('id', $addid)->update(['status' => '1']);

            if ($result === false)
            {
                $this->error('更新默认地址失败');
                exit;
            }

            $this->success('更新成功');
            exit;
        }
    }

    // 详情
    public function info()
    {
        if ($this->request->isPost())
        {
            $addid = $this->request->param('addid', 0, 'trim');

            $Address = $this->AddressModel->find($addid);

            if (!$Address)
            {
                $this->error('收件地址不存在');
                exit;
            }

            $this->success('','', $Address);
        }
    }

    // 编辑
    public function edit()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->param();
            $code = isset($params['code']) ? $params['code'] : '';
            $addid = isset($params['addid']) ? $params['addid'] : 0;

            $Address = $this->AddressModel->find($addid);

            if (!$Address)
            {
                $this->error('收件地址不存在');
                exit;
            }

            $data = [
                'id' => $addid,
                'consignee' => $params['consignee'] ?? '',
                'address' => $params['address'] ?? '',
                'mobile' => $params['mobile'] ?? '',
                'status' => $params['status'] ?? '',
            ];

            $parentpath = $this->RegionModel->where('code', $code)->value('parentpath');

            if (!$parentpath)
            {
                $this->error('地区不存在，请重新选择');
                exit;
            }

            $path = explode(',', $parentpath);
            $data['province'] = $path[0] ?? '';
            $data['city'] = $path[1] ?? '';
            $data['district'] = $path[2] ?? '';

            // 判断是否添加默认地址
            if ($params['status'] == '1')
            {
                $ids = $this->AddressModel->where(['busid' => $this->busid, 'status' => '1'])->value('id');

                if ($ids)
                {
                    $res = $this->AddressModel->where('id', $ids)->update(['status' => '0']);

                    if (!$res)
                    {
                        $this->error('更新默认地址失败');
                        exit;
                    }
                }
            }

            // 校验器
            $result = $this->AddressModel->isUpdate(true)->save($data);

            if ($result === false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }

            $this->success('更新成功');
            exit;
        }
    }

    // 删除
    public function del()
    {
        if ($this->request->isPost())
        {
            $addid = $this->request->param('addid', 0, 'trim');

            $Address = $this->AddressModel->find($addid);

            if (!$Address)
            {
                $this->error('收件地址不存在');
                exit;
            }

            $result = $this->AddressModel->destroy($addid);

            if($result === false)
            {
                $this->error($this->AddressModel->getError());
                exit;
            }

            $this->success('删除成功');
            exit;
        }
    }

    // 订单地址
    public function order()
    {
        if ($this->request->isPost())
        {
            $data = $this->AddressModel->where(['busid' => $this->busid, 'status' => '1'])->find();

            $this->success('','', $data);
        }
    }
}
